<?php
/**
 * Created by PhpStorm.
 * User: chl
 * Date: 2017/3/16 0016
 * Time: 12:12
 */
namespace app\admin\controller;
use app\models\Bases;
use think\Request;

class Group extends Base{
    public function groupList(){
        $ruleModel = new Bases('authRule');
        $groupModel = new Bases('authGroup');
        $ruleList = $groupModel->selectData();
        foreach($ruleList as $k=>$v){
            $result['list'][$k]['id'] = $v['id'];
            $result['list'][$k]['title'] = $v['title'];
            $result['list'][$k]['status'] = $v['status'];
            $result['list'][$k]['privilege'] = $ruleModel->selectData(['id'=>['in',$v['rules']]]);
        }
        return view('groupList',['result'=>$result]);
    }
    public function groupAdd(){
        $ruleModel = new Bases('authRule');
        $groupModel = new Bases('authGroup');
        try{
            $list = $ruleModel->selectData('p_id =0 ');//获取所有顶级分类并查找所有子集分类
            foreach($list as $v){
                $result['list'][$v['title']] = $ruleModel->selectData('p_id ='.$v['id']);
                //以权限名称作为数组$result['list']的下标
                //并且获取该权限下面的子权限
            }
            if(Request()->isPost()){
                $post = input('param.');

                if(empty($post['ids'])){
                    throw new \LogicException('没有选择权限',4000);
                }
                $validate = $this->validate($post,'Group.groupAdd');//验证
                if($validate !== true){
                    throw new \LogicException($validate,10000);
                }
                $post['rules'] = implode(',',$post['ids']);
                unset($post['ids']);
                $rs = $groupModel->insertData($post);
                if($rs){
                    $groupModel->addAdminLog('添加'.$post['title'].'角色');
                    return self::formatSuccessResult();
                }return self::formatResult(106,'操作失败');
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('groupAdd',['result'=>$result]);
    }

    public function groupSave(){
        $groupModel = new Bases('authGroup');
        $ruleModel = new Bases('authRule');
        try{
            $id = input('id');
            $result['one'] = $groupModel->find(['id'=>$id]);
            $ruleInfo = $ruleModel->selectData('p_id =0');
            foreach($ruleInfo as $v){
                $result['list'][$v['title']] = $ruleModel->selectData(['p_id'=>$v['id']]);
            }

            if(Request()->isPost()){
                $post = input('param.');
                if(empty($post['ids'])){
                    throw new \LogicException('没有选择权限',4000);
                }
                $post['rules'] = implode(',',$post['ids']);
                unset($post['ids']);
                $rs = $groupModel->updateData($post,['id'=>$post['id']]);
                if($rs !== false){
                    $groupModel->addAdminLog('修改'.$post['title'].'角色');
                    return self::formatSuccessResult();
                }return self::formatResult(406,'操作失败');

            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }

        return view('groupSave',['result'=>$result]);
    }
}